/**
 * @description 获取当前路由信息
 * @author xfj
 */

import { useContext } from 'react'
import { useLocation, UNSAFE_NavigationContext } from 'react-router-dom'
import { routes } from '../router/routes'

function useCurrentRoute() {
  const location = useLocation()
  const pathname = location.pathname
  const { navigator } = useContext(UNSAFE_NavigationContext)
  const matchRoutes = []
  // let currentRoute;
  let auth = false;

  routes.forEach(item => {
    if(item.path === location.pathname) {
      // currentRoute = item
      auth = item.auth === void 0 ? auth : item.auth
    } else if(pathname.startsWith(item.path)) {
      matchRoutes.push(item)
      auth = item.auth === void 0 ? auth : auth || item.auth
    }
  })
  return {
    // currentRoute,
    fullPath: navigator.createHref(location),
    matchRoutes,
    auth,
  }
}

export default useCurrentRoute
